<template>
  <div class="header">
    <div class="header-left">
      <el-button 
        text 
        class="collapse-btn"
        @click="toggleSidebar"
      >
        <el-icon size="22">
          <Expand v-if="isCollapse" />
          <Fold v-else />
        </el-icon>
      </el-button>
      

    </div>
    
    <div class="header-right">
      <!-- 搜索 -->
      <el-input
        v-model="searchValue"
        placeholder="搜索文档、知识库..."
        class="search-input"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      
      <!-- 通知 -->
      <el-badge :value="12" class="notification-badge">
        <el-button text class="notification-btn">
          <el-icon size="24"><Bell /></el-icon>
        </el-button>
      </el-badge>
      
      <!-- 用户信息 -->
      <el-dropdown>
        <span class="user-info">
          <el-avatar size="small" :src="adminStore.userAvatar" class="user-avatar">
            <el-icon><User /></el-icon>
          </el-avatar>
          <span class="username">{{ adminStore.userName }}</span>
          <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="goToProfile">
              <el-icon><User /></el-icon>
              个人中心
            </el-dropdown-item>
            <el-dropdown-item>
              <el-icon><Setting /></el-icon>
              系统设置
            </el-dropdown-item>
            <el-dropdown-item divided @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAdminStore } from '@/stores/admin'
import { Bell, User, Setting, SwitchButton, Search, ArrowDown ,Expand,Fold} from '@element-plus/icons-vue'

// 定义 props 接收 isCollapse 状态和 toggle 方法
defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['toggle-sidebar'])

// 切换侧边栏状态
const toggleSidebar = () => {
  emit('toggle-sidebar')
}
// 从父组件接收的props
// const route = useRoute()
const router = useRouter()
const adminStore = useAdminStore()

// 跳转到个人中心
const goToProfile = () => {
  router.push('/userView')
}

// 退出登录
const handleLogout = async () => {
  try {
    adminStore.logout()
    window.dispatchEvent(new Event('user-logout'))
    router.push('/')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}

// 响应式数据
const searchValue = ref('')


// // 计算属性：获取当前用户信息
// const currentUser = computed(() => adminStore.user)
// const userAvatar = computed(() => adminStore.userAvatar || '')

// // 路由名称映射
// const routeNames = {
//   '/dashboard': '工作台',
//   '/documentList': '文档管理',
//   '/knowledgeBase': '知识库管理',
//   '/sessionLog': '对话记录',
//   '/sessionStats': '对话统计分析',
//   '/userView': '个人中心'
// }

</script>

<style scoped>
.header {
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95) 0%, rgba(22, 33, 62, 0.95) 100%);
  border-bottom: 1px solid rgba(0, 212, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  height: 72px;
  flex-shrink: 0;
  width: 100%;
  box-sizing: border-box;
  box-shadow: 0 4px 20px rgba(0, 212, 255, 0.1);
  position: relative;
  z-index: 5;
  border-radius: 16px 16px 0 0;
  backdrop-filter: blur(20px);
}

/* 添加科技感装饰元素 */
.header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, #00d4ff 50%, transparent 100%);
  opacity: 0.6;
}

.header::after {
  content: '';
  position: absolute;
  top: 50%;
  right: 20px;
  width: 60px;
  height: 60px;
  background: radial-gradient(circle, rgba(0, 212, 255, 0.1) 0%, transparent 70%);
  border-radius: 50%;
  transform: translateY(-50%);
  pointer-events: none;
}

.header-left {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.collapse-btn {
  margin-right: 24px;
  padding: 0;
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  color: #ffffff;
  background: rgba(0, 212, 255, 0.2);
  border: 1px solid rgba(0, 212, 255, 0.3);
}

.collapse-btn:hover {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  color: #ffffff;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 212, 255, 0.3);
  border-color: #00d4ff;
}

.collapse-btn:active {
  transform: translateY(0) scale(0.96);
}

.collapse-btn .el-icon {
  font-size: 24px;
}



.header-right {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-shrink: 0;
}

.search-input {
  width: 280px;
  flex-shrink: 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

:deep(.el-input__wrapper) {
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.8) 0%, rgba(22, 33, 62, 0.8) 100%);
  box-shadow: 0 0 0 1px rgba(0, 212, 255, 0.3);
  height: 44px;
  padding: 0 16px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.5);
  background: rgba(26, 26, 46, 0.9);
  transform: translateY(-1px);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 3px rgba(0, 212, 255, 0.3) !important;
  background: rgba(26, 26, 46, 0.9);
  transform: translateY(-2px);
}

:deep(.el-input__prefix) {
  color: rgba(255, 255, 255, 0.7);
  margin-right: 12px;
}

:deep(.el-input__inner) {
  font-size: 15px;
  color: #ffffff;
  font-weight: 500;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

.notification-badge {
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.3s ease;
}

.notification-badge:hover {
  transform: translateY(-2px);
}

.notification-btn {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: rgba(0, 212, 255, 0.2);
  border: 1px solid rgba(0, 212, 255, 0.3);
  transition: all 0.3s ease;
  color: #ffffff;
}

.notification-btn:hover {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  color: #ffffff;
  border-color: #00d4ff;
  box-shadow: 0 8px 20px rgba(0, 212, 255, 0.3);
}

:deep(.el-badge__content) {
  border: 2px solid #ffffff;
  font-weight: 700;
  height: 20px;
  min-width: 20px;
  background: linear-gradient(135deg, #ff4757 0%, #c44569 100%);
  box-shadow: 0 2px 8px rgba(255, 71, 87, 0.3);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 8px 16px 8px 12px;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
  flex-shrink: 0;
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%);
  border: 1px solid rgba(0, 212, 255, 0.3);
}

.user-info:hover {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  border-color: #00d4ff;
  box-shadow: 0 8px 20px rgba(0, 212, 255, 0.3);
  transform: translateY(-2px);
}

.user-info:hover .username {
  color: #ffffff;
}

.user-info:hover .dropdown-icon {
  color: #ffffff;
}

.username {
  font-size: 15px;
  font-weight: 600;
  color: #ffffff;
  letter-spacing: 0.3px;
  transition: all 0.3s ease;
}

.dropdown-icon {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.el-dropdown:hover .dropdown-icon {
  transform: rotate(180deg);
}

/* Dropdown menu styling */
:deep(.el-dropdown-menu) {
  border-radius: 16px;
  padding: 8px 0;
  box-shadow: 0 20px 40px rgba(0, 212, 255, 0.2);
  border: 1px solid rgba(0, 212, 255, 0.3);
  margin-top: 8px;
  backdrop-filter: blur(20px);
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95) 0%, rgba(22, 33, 62, 0.95) 100%) !important;
}

:deep(.el-dropdown-menu__item) {
  padding: 12px 24px;
  font-size: 15px;
  color: #ffffff;
  transition: all 0.3s ease;
  font-weight: 500;
  margin: 2px 8px;
  border-radius: 8px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 12px;
  font-size: 18px;
  width: 20px;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
}

:deep(.el-dropdown-menu__item:hover) {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  color: #ffffff;
  transform: translateX(4px);
}

:deep(.el-dropdown-menu__item:hover .el-icon) {
  color: #ffffff;
}

:deep(.el-dropdown-menu__item--divided) {
  border-top: 1px solid rgba(0, 212, 255, 0.3);
  margin-top: 8px;
  padding-top: 8px;
}

/* Avatar styling */
:deep(.el-avatar) {
  transition: all 0.3s ease;
  border: 2px solid rgba(0, 212, 255, 0.3);
}

.user-info:hover :deep(.el-avatar) {
  border-color: #ffffff;
  transform: scale(1.05);
}

/* Responsive adjustments */
@media (max-width: 1200px) {
  .header {
    padding: 0 24px;
  }
  
  .search-input {
    width: 240px;
  }
  
  .header-right {
    gap: 20px;
  }
}

@media (max-width: 992px) {
  .header {
    padding: 0 20px;
  }
  
  .search-input {
    width: 200px;
  }
  
  .header-right {
    gap: 16px;
  }
  
  .collapse-btn {
    margin-right: 16px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 16px;
    height: 64px;
  }
  
  .search-input {
    width: 160px;
  }
  
  .username {
    display: none;
  }
  
  .user-info {
    padding: 8px 12px 8px 8px;
  }
  

  
  .header-right {
    gap: 12px;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 0 12px;
  }
  
  .search-input {
    width: 120px;
  }
  
  .collapse-btn {
    margin-right: 8px;
    width: 40px;
    height: 40px;
  }
  
  .collapse-btn .el-icon {
    font-size: 20px;
  }
  
  .header-right {
    gap: 8px;
  }
}
</style>